import {
  UserPhotoRespones,
  UserProfile,
  UserProRespones,
  UserRespones,
} from '@/types/data'
import { RootThunkAction } from '@/types/store'
import { http } from '@/utils/http'

// 接收相应数据类型

export const getUser = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserRespones>('/user')
    console.log(res)

    dispatch({ type: 'User/get', payload: res.data.data })
  }
}

// 获取个人信息页面数据
export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserProRespones>('/user/profile')

    dispatch({ type: 'profile/getUserProfile', payload: res.data.data })
  }
}

// 更新昵称、简介、性别、生日  工具泛型函数Partial 返回可选类型
export const getUpdataProfile = (
  userProfile: Partial<UserProfile>
): RootThunkAction => {
  return async (dispatch) => {
    await http.patch('/user/profile', userProfile)

    dispatch({ type: 'Profile/Updata', payload: userProfile })
  }
}

// 更新个人头像
export const onUpdataPhoto = (file: FormData): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.patch<UserPhotoRespones>('/user/photo', file)
    console.log(res)
  }
}
